/*
 * GwtYuiCarousel - API to integrate YUI Carousel in GWT framework
 *
 * Copyright (c) 2007, Nicolas Hoppenot
 *
 * This free software is licensed under the Apache License, Version 2.0 (the "License").
 * To obtain a copy of the licence, visit : http://www.apache.org/licenses/LICENSE-2.0
 *
 * This software is distributed WITHOUT ANY WARRANTY OR CONDITION OF ANY KIND.
 * Please see the license for more information.
 *
 * Version $0.6.1-beta1$
 */
package com.gwtyuicarousel.javascriptload.client;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.gwtyuicarousel.client.ui.Carousel;
import com.gwtyuicarousel.client.ui.CarouselConfig;
import com.gwtyuicarousel.client.ui.CarouselListener;

/**
 * TODO Add A comment
 * 
 * @version $0.6.1-beta1$
 * @author Nicolas Hoppenot
 * 
 */
public class JavaScriptLoadCarousel
    extends Carousel implements CarouselListener {

  /**
   * List of carousel contents
   * 
   * @gwt.typeArgs <com.gwtyuicarousel.javascriptload.client.ContentDefinition>
   */
  private static List contents;

  static {
    contents = new ArrayList();
    contents
        .add(new ContentDefinition(
            "http://lh5.google.fr/nicolas.hoppenot/Rthv0cuTFSI/AAAAAAAAACg/SjDU_JAlnOs/s144/Photo%20113.jpg",
            "http://picasaweb.google.fr/nicolas.hoppenot/Annecy/photo#5104953124478719266"));
    contents
        .add(new ContentDefinition(
            "http://lh6.google.fr/nicolas.hoppenot/RthwBsuTFTI/AAAAAAAAACs/aVyV7fFBrUw/s144/Photo%20121.jpg",
            "http://picasaweb.google.fr/nicolas.hoppenot/Annecy/photo#5104953352111985970"));
    contents
        .add(new ContentDefinition(
            "http://lh3.google.fr/nicolas.hoppenot/RthwC8uTFUI/AAAAAAAAAC0/rRR2qh2bUEA/s144/Photo%20027.jpg",
            "http://picasaweb.google.fr/nicolas.hoppenot/Annecy/photo#5104953373586822466"));
    contents
        .add(new ContentDefinition(
            "http://lh5.google.fr/nicolas.hoppenot/RthwScuTFVI/AAAAAAAAAC8/2SxrNXIWjG8/s144/Photo%20051.jpg",
            "http://picasaweb.google.fr/nicolas.hoppenot/Annecy/photo#5104953639874794834"));
    contents
        .add(new ContentDefinition(
            "http://lh5.google.fr/nicolas.hoppenot/RthwhcuTFWI/AAAAAAAAADE/igriguza5EM/s144/Photo%20052.jpg",
            "http://picasaweb.google.fr/nicolas.hoppenot/Annecy/photo#5104953897572832610"));
    contents
    .add(new ContentDefinition(
        "http://lh6.google.fr/nicolas.hoppenot/RthwxsuTFXI/AAAAAAAAADM/UVAf3nbf270/s144/Photo%20055.jpg",
        "http://picasaweb.google.fr/nicolas.hoppenot/Annecy/photo#5104954176745706866"));
    contents
    .add(new ContentDefinition(
        "http://lh4.google.fr/nicolas.hoppenot/RthxEMuTFYI/AAAAAAAAADU/Y9hT_SIYvJI/s144/Photo%20056.jpg",
        "http://picasaweb.google.fr/nicolas.hoppenot/Annecy/photo#5104954494573286786"));
    contents
    .add(new ContentDefinition(
        "http://lh3.google.fr/nicolas.hoppenot/RthxW8uTFZI/AAAAAAAAADc/cLwd2s0U6hk/s144/Photo%20061.jpg",
        "http://picasaweb.google.fr/nicolas.hoppenot/Annecy/photo#5104954816695834002"));
    contents
    .add(new ContentDefinition(
        "http://lh6.google.fr/nicolas.hoppenot/RthxlsuTFaI/AAAAAAAAADo/716ija-VrE4/s144/Photo%20062.jpg",
        "http://picasaweb.google.fr/nicolas.hoppenot/Annecy/photo#5104955070098904482"));
    contents
    .add(new ContentDefinition(
        "http://lh5.google.fr/nicolas.hoppenot/Rthx3cuTFbI/AAAAAAAAADw/QadN6zg32Vo/s144/Photo%20082.jpg",
        "http://picasaweb.google.fr/nicolas.hoppenot/Annecy/photo#5104955375041582514"));
    contents
    .add(new ContentDefinition(
        "http://lh6.google.fr/nicolas.hoppenot/RthyFsuTFcI/AAAAAAAAAD4/FzFcbshvUYk/s144/Photo%20122.jpg",
        "http://picasaweb.google.fr/nicolas.hoppenot/Annecy/photo#5104955619854718402"));
    contents
    .add(new ContentDefinition(
        "http://lh4.google.fr/nicolas.hoppenot/RthyYMuTFdI/AAAAAAAAAEA/iZWzjpskUPg/s144/Photo%20012.jpg",
        "http://picasaweb.google.fr/nicolas.hoppenot/Annecy/photo#5104955937682298322"));
    contents
    .add(new ContentDefinition(
        "http://lh6.google.fr/nicolas.hoppenot/RthypsuTFeI/AAAAAAAAAEI/GmA5yjJQ7y8/s144/Photo%20013.jpg",
        "http://picasaweb.google.fr/nicolas.hoppenot/Annecy/photo#5104956238330009058"));
    contents
    .add(new ContentDefinition(
        "http://lh3.google.fr/nicolas.hoppenot/Rthy18uTFfI/AAAAAAAAAEQ/41fVCSNY6jE/s144/Photo%20021.jpg",
        "http://picasaweb.google.fr/nicolas.hoppenot/Annecy/photo#5104956448783406578"));
    contents
    .add(new ContentDefinition(
        "http://lh6.google.fr/nicolas.hoppenot/RthzFsuTFgI/AAAAAAAAAEc/reZPCOAzm18/s144/Photo%20089.jpg",
        "http://picasaweb.google.fr/nicolas.hoppenot/Annecy/photo#5104956719366346242"));
    contents
    .add(new ContentDefinition(
        "http://lh4.google.fr/nicolas.hoppenot/RthzUMuTFhI/AAAAAAAAAEk/bxg9Ix6QtsU/s144/Photo%20090.jpg",
        "http://picasaweb.google.fr/nicolas.hoppenot/Annecy/photo#5104956968474449426"));
  }

  private int lastContentIndex = -1;

  /**
   * TODO Add a comment
   */
  public JavaScriptLoadCarousel(String elementId, CarouselConfig config) {
    super(elementId, config);
    setCarouselListener(this);
  }

  public void load(int startIndex, int lastIndex) {
    for (int index = startIndex; index <= lastIndex; index++) {
      lastContentIndex = getRandom(contents.size() - 1, lastContentIndex);
      ContentDefinition content = (ContentDefinition) contents.get(lastContentIndex);
      addItem(index, getItemInnerHtml(content.getThumbUrl(), content.getImageUrl(), "Number "
          + (index + 1)));
    }
  }

  /**
   * Construct the HTML describing the element
   * 
   * @param thumbUrl
   *          The url of the thumb
   * @param photoUrl
   *          The url of the photo
   * @param title
   *          The title of the photo
   * @return The HTML String of the HTML element
   */
  private String getItemInnerHtml(String thumbUrl, String photoUrl, String title) {
    StringBuffer innerHtml = new StringBuffer("<a href=\"");
    innerHtml.append(photoUrl).append("\"><img src=\"").append(thumbUrl).append("\" width=\"");
    innerHtml.append("75").append("\" height=\"").append("75").append("\"/>").append(title);
    innerHtml.append("</a>");
    return innerHtml.toString();
  }

  /**
   * Get a random index for the content
   * 
   * @param max
   *          The maximum possible index
   * @param last
   *          The index of the last content displayed
   * @return The random index
   */
  private int getRandom(int max, int last) {
    while (true) {
      int randomIndex = (int) Math.floor(Math.random() * max);
      if (randomIndex != last) {
        return randomIndex;
      }
    }
  }

  /**
   * @see com.gwtyuicarousel.client.ui.CarouselListener#onAnimationComplete(com.gwtyuicarousel.client.ui.Carousel,
   *      com.gwtyuicarousel.client.ui.Carousel.Direction)
   */
  public void onAnimationComplete(Carousel carousel, Direction direction) {
    // In this example, there is nothing to do when this event is handled
  }

  /**
   * @see com.gwtyuicarousel.client.ui.CarouselListener#onLoadInit(com.gwtyuicarousel.client.ui.Carousel,
   *      int, int, boolean)
   */
  public void onLoadInit(Carousel carousel, int startIndex, int lastIndex, boolean alreadyCached) {
    load(startIndex, lastIndex);
  }

  /**
   * @see com.gwtyuicarousel.client.ui.CarouselListener#onLoadNext(com.gwtyuicarousel.client.ui.Carousel,
   *      int, int, boolean)
   */
  public void onLoadNext(Carousel carousel, int startIndex, int lastIndex, boolean alreadyCached) {
    if (!alreadyCached) {
      load(startIndex, lastIndex);
    }
  }

  /**
   * @see com.gwtyuicarousel.client.ui.CarouselListener#onLoadPrevious(com.gwtyuicarousel.client.ui.Carousel,
   *      int, int, boolean)
   */
  public void onLoadPrevious(Carousel carousel, int startIndex, int lastIndex, boolean alreadyCached) {
    if (!alreadyCached) {
      load(startIndex, lastIndex);
    }
  }

  /**
   * @see com.gwtyuicarousel.client.ui.CarouselListener#onNextButtonStateChange(com.gwtyuicarousel.client.ui.Carousel,
   *      com.google.gwt.user.client.Element, boolean)
   */
  public void onNextButtonStateChange(Carousel carousel, Element[] nextElements, boolean enabled) {
    // Nothing to do
  }

  /**
   * @see com.gwtyuicarousel.client.ui.CarouselListener#onPrevButtonStateChange(com.gwtyuicarousel.client.ui.Carousel,
   *      com.google.gwt.user.client.Element, boolean)
   */
  public void onPrevButtonStateChange(Carousel carousel, Element[] previousElements, boolean enabled) {
    // When we are in the first image, the HTML element of the previous element change
    for (int index = 0; index < previousElements.length; index++) {
      if (enabled) {
        DOM
            .setElementAttribute(previousElements[index], "src",
                "resources/images/left-enabled.gif");
      } else {
        DOM.setElementAttribute(previousElements[index], "src",
            "resources/images/left-disabled.gif");
      }
    }
  }
}
